from gen import TreeNode

class Solution:
    def levelOrder(self, root: TreeNode):
        """
        使用分治算法实现二叉树的层序遍历
        """
        if not root:
            return []
        
        # 递归处理左子树和右子树
        left_result = self.levelOrder(root.left)
        right_result = self.levelOrder(root.right)
        
        # 合并结果
        result = [[root.val]]
        
        for i in range(max(len(left_result), len(right_result))):
            current_level = []
            
            if i < len(left_result):
                current_level.extend(left_result[i])
                
            if i < len(right_result):
                current_level.extend(right_result[i])
                
            if current_level:
                result.append(current_level)
                
        return result